package com.tvein.web.view
{
	//flash
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.filters.BitmapFilter;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.external.ExternalInterface;
	//pureMVC
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	//custom
	import com.tvein.web.common.CommonConstants;
	import com.tvein.Web;
	import com.tvein.web.swc.logo.New3g;
	import com.tvein.web.swc.background.BackGround;
	import com.tvein.web.swc.toolBar.ToolBar;
//
    public class WebMediator extends Mediator implements IMediator
    {
		public static const NAME:String = 'WebMediator';
		
		public function WebMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		public function get web() : Web
		{
			return viewComponent as Web;
		}
		
		//Custom
		private var logTime:int = 0;
		private var checkLoad:Boolean = false;
		private var status:int = 0;
		private var fileNum:int = 0;
		
		private var logo:New3g;
		private var bg:BackGround;
		private var toolbar:ToolBar;
		
		override public function onRegister() : void
        {
			bg = new BackGround();
			logo = new New3g();
			toolbar = new ToolBar();
			
			toolbar.ToolText("= 点击鼠标加载游戏资源 =");
			
			web.addChild(bg);
			web.addChild(logo);
			web.addChild(toolbar);
			
			drawUI();
			
			var filterTimer:Timer = new Timer(100);
			filterTimer.addEventListener(TimerEvent.TIMER, onFilter);
			filterTimer.start();
			
			web.stage.addEventListener(Event.RESIZE, resizeUI);
			web.stage.addEventListener(MouseEvent.CLICK, onClick);
        }
		
		override public function listNotificationInterests() : Array
		{
			var interests:Array = super.listNotificationInterests();
			interests.push(CommonConstants.FILELOADER_LOADOK);
			return interests;
		}
		
		override public function handleNotification(notification:INotification) : void
		{
			var progress:Array;
			var txt:String;
			
			switch(notification.getName())
			{
				case CommonConstants.FILELOADER_LOADOK:
					
					fileNum = fileNum + 1;
					
					txt = ".. 加载中(" + int(fileNum/21 * 100) + "%) ..\n";
					
					for (var i:int = 0; i < fileNum; i++)
					{
						if ((i % 2) == 1)
						{
							txt = txt + "■";
						}
					}
					
					toolbar.ToolText(txt);
					
					if(notification.getBody() == "/SanGuo/preload.xml")
					{
						toolbar.ToolText("- 点击鼠标开始游戏 -");
						status = 2;
					}
					else
					{
						logTime = 0;
						checkLoad = false;
					}
					break;
			}
		}
		
		private function drawUI() : void
		{
			bg.height = web.stage.stageHeight;
			bg.width = web.stage.stageWidth;
			
			logo.height = web.stage.stageHeight * .2;
			logo.width = (logo.height) * 3;

			logo.x = (web.stage.stageWidth - logo.width) / 2;
			logo.y = (web.stage.stageHeight - logo.height) / 2 - 50;
			
			toolbar.width = web.stage.stageWidth;
			
			toolbar.x = (web.stage.stageWidth - toolbar.width) / 2;
			toolbar.y = web.stage.stageHeight - 100;
		}
		
		private function onClick(evt:MouseEvent) : void
		{
			switch(status)
			{
				case 0:
					toolbar.ToolText("请允许左下角弹出的“对等协助网络”申请，以便利用p2p技术进行游戏加速");
					sendNotification(CommonConstants.FILELOADER, ["/fileloader -P", "/SanGuo/preload.xml"]);
					
					status = 1;
					break;
				case 1:
					break;
				case 2:
					onStart();
					break;
			}
		}
		
		private function resizeUI(evt:Event) : void
		{
			drawUI();
		}
		
		private function onStart():void
		{
			web.stage.removeEventListener(Event.RESIZE, resizeUI);
			web.stage.removeEventListener(MouseEvent.CLICK, onClick);
			
			web.removeChild(bg);
			web.removeChild(logo);
			web.removeChild(toolbar);
			
			ExternalInterface.call("GamePlay", "/SanGuo/SanGuo.swf");
			
			//sendNotification(CommonConstants.SYSTEM, ["/system -G", "/SanGuo/SanGuo.swf"]);
		}

		private var filterArray:Array;
		private var filter:BitmapFilter;
		
		private var light:Boolean = false;
		private var alpha:Number = 0;
		private	var color:Number = 0xffffff;
		private var inner:Boolean = false;
        private var knockout:Boolean = false;
        private var quality:Number = BitmapFilterQuality.MEDIUM;
		
		private function onFilter(evt:TimerEvent):void
		{
			var blurX:Number = Math.round(200);
			var blurY:Number = Math.round(200);
			var strength:Number = Math.round(10);
			
			if (light == false)
			{
				if (alpha < .6)
				{
					alpha = alpha + 0.06;
				}
				else
				{
					light = true;
				}
			}
			else
			{
				if (alpha > 0)
				{
					alpha = alpha - 0.06;
				}
				else
				{
					light = false;
				}
			}
			
			filterArray = new Array();
			
			filter = new GlowFilter(color, alpha, blurX, blurY, strength, quality, inner, knockout);
			filterArray.push(filter);
			logo.filters = filterArray;
			
			if (status == 1)
			{
				logTime += 1;
				
				if (logTime > 200)
				{
					if (!checkLoad)
					{
						toolbar.ToolText(toolbar.GetText() + "\n 加载时间过久，您可以试着刷新重新连接");

						checkLoad = true;
					}
				}
			}
		}
    }
}